package gui;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.xml.sax.SAXException;

import ctrl.EVECentralCtrl;
import util.Constants.EVEData;
import data.EVEItem;
import data.EVESystem;
import data.ItemSystemCollection;

public class ResultDialog extends JDialog implements MouseListener, ActionListener {

  private String[]             _selectedEntry;
  private CollectionTabbedPane _collectionPane;
  private EVECentralCtrl       _evecctrl;

  private JButton              _okButton;
  private JButton              _cancelButton;

  private JList<String>        _resultList;
  private ArrayList<String[]>  _results;

  public ResultDialog(EVEData data, ArrayList<String[]> results,
      CollectionTabbedPane collectionPane, EVECentralCtrl evecctrl) {
    super();
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    setTitle("Results");

    _collectionPane = collectionPane;
    _results = results;
    _evecctrl = evecctrl;
    DefaultListModel<String> resultModel = new DefaultListModel<String>();

    for (int i = 0; i < results.size(); i++) {
      resultModel.add(i, results.get(i)[2]);
    }

    _resultList = new JList<String>(resultModel);
    _resultList.addMouseListener(this);
    JScrollPane listScrollPane = new JScrollPane(_resultList);
    mainPanel.add(listScrollPane);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    _okButton = new JButton("OK");
    _cancelButton = new JButton("Cancel");
    _cancelButton.addActionListener(this);
    _okButton.setEnabled(false);
    buttonPanel.add(_okButton);
    buttonPanel.add(_cancelButton);
    mainPanel.add(buttonPanel);

    if (data == EVEData.Systems) {
      _okButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
          try {
            ItemSystemCollection collection = _collectionPane.getSelectedCollection();
            EVESystem newSystem = new EVESystem(_selectedEntry[0], _selectedEntry[1], _selectedEntry[2]);
            boolean exists = false;
            for (EVESystem oldSystem : collection.getSystems()) {
              if (newSystem.getSystemID().equals(oldSystem.getSystemID())) {
                exists = true;
              }
            }
            if (!exists) {
              _collectionPane.addSystem(newSystem);
              for (EVEItem item : collection.getItems()) {
                item._addSystem(newSystem);
              }
              setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
              _evecctrl._retrieveMarketData(newSystem);
              setCursor(Cursor.getDefaultCursor());
            }
            else {
              System.out.println("System already exists");
            }
            _dispose();
          }
          catch (IOException | SAXException e1) {
            e1.printStackTrace();
          }
        }
      });
    }
    else if (data == EVEData.Items) {
      _okButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
          try {
            ItemSystemCollection collection = _collectionPane.getSelectedCollection();
            EVEItem newItem = new EVEItem(_selectedEntry[0], _selectedEntry[1], _selectedEntry[2]);
            boolean exists = false;
            for (EVEItem oldItem : collection.getItems()) {
              if (newItem.getItemID().equals(oldItem.getItemID())) {
                exists = true;
              }
            }
            if (!exists) {
              _collectionPane.addItem(newItem);
              for (EVESystem system : collection.getSystems()) {
                system._addItem(newItem);
              }
              setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
              _evecctrl._retrieveMarketData(newItem);
              setCursor(Cursor.getDefaultCursor());
            }
            else {
              System.out.println("Item already exists");
            }
            _dispose();
          }
          catch (IOException | SAXException e1) {
            e1.printStackTrace();
          }
        }
      });
    }
    else {
      _dispose();
    }

    add(mainPanel);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
  }

  public String[] _getSelectedItem() {
    if (_resultList.getSelectedIndex() == -1) {
      return null;
    }
    else {
      return _results.get(_resultList.getSelectedIndex());
    }
  }

  public void _dispose() {
    setVisible(false);
    dispose();
  }

  @Override
  public void mouseClicked(MouseEvent e) {
  }

  @Override
  public void mouseEntered(MouseEvent e) {
  }

  @Override
  public void mouseExited(MouseEvent e) {
  }

  @Override
  public void mousePressed(MouseEvent e) {
    _selectedEntry = _getSelectedItem();
    if (_selectedEntry != null) {
      _okButton.setEnabled(true);
    }
  }

  @Override
  public void mouseReleased(MouseEvent e) {
  }

  @Override
  public void actionPerformed(ActionEvent event) {
    setVisible(false);
    dispose();
  }
}